/**********************, *****************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: figure_F1_exportcorrection.do
Date: October 2022

Description: Reproduces Figure F.1: Firm-embedded productivity and export corrections.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/figure_F1_exportcorrection.log",replace
*-------------------------------------------------------------------------------





*Manufacturing Only: adjusting the baseline to have the same firms as the one excluding exports
*--------------------------------------------------------------------------
clear all
use "${data}/estimates_aggtype_naics_sales_s1_robust_woparent_exclexp.dta", clear

rename D_A xx
rename D_A_se xx_se
merge 1:1 year isocode type using "${data}/estimates_aggtype_naics_sales_s1_base_woparent_exclexp.dta", keepusing(D_A D_A_se)
drop _merge
keep if type=="manuf"
rename D_A DA_base
rename D_A_se DA_se_base
rename xx DA_exp
rename xx_se DA_se_exp

gen dummy=0
foreach xx in DE EE FR GB GR HR HU SI {
display "`xx'"
replace dummy=1 if isocode=="`xx'"
}
keep if dummy==1

gen u_DA_base = DA_base + 1.96 * DA_se_base
gen l_DA_base = DA_base - 1.96 * DA_se_base 
gen u_DA = DA_exp + 1.96 * DA_se_exp 
gen l_DA = DA_exp - 1.96 * DA_se_exp 

keep year isocode type u_DA_base l_DA_base u_DA l_DA DA_base DA_exp

gsort DA_base
gen num=1 if isocode=="EE"
replace num=2 if isocode=="SI"
replace num=3 if isocode=="HR"
replace num=4 if isocode=="HU"
replace num=5 if isocode=="GR"
replace num=6 if isocode=="GB"
replace num=7 if isocode=="FR"
replace num=8 if isocode=="DE"
gsort -num

drop year
gen t=_n
sum t

sum t
forvalues ii=1(1)`r(max)' {
local ctry`ii' = isocode[`ii']
local obs`ii' = `ii'
display "`ctry`ii'' ; `obs`ii''"
}

twoway rcap u_DA_base l_DA_base t, lc(red) /// 
||     rcap u_DA l_DA t, lc(blue) /// 
||     scatter DA_base t, mc(red)  /// 
||     scatter DA_exp t, mc(blue)  ///
yline(0) yline(0) ylab(-2(1)4) xlab(1(1)8)  xlabel(, valuelabel) xlabel(, angle(45)) ytitle("") xtitle("") graphregion(fcolor(white) lcolor(white)) xlabel(`obs1' "`ctry1'" `obs2' "`ctry2'"  `obs3' "`ctry3'" `obs4' "`ctry4'" `obs5' "`ctry5'" `obs6' "`ctry6'" `obs7' "`ctry7'" `obs8' "`ctry8'", labsize(small)) legend(order(3 "Baseline (adjusted)" 4 "Export correction") rows(1) size(small) bplace(n) ring(0) region(lwidth(none)))
display "fig1_`title'_`yy'"
graph export "${rappendix}/fig_F1_exclexports_manufacturing.pdf", replace



*Aggregate: adjusting the baseline to have the same firms as the one excluding exports
*--------------------------------------------------------------------------
clear all
use "${data}/estimates_agg_naics_sales_s1_robust_woparent_exclexp.dta", clear

rename D_A xx
rename D_A_se xx_se
merge 1:1 year isocode using "${data}/estimates_agg_naics_sales_s1_base_woparent_exclexp.dta", keepusing(D_A D_A_se)
drop _merge

rename D_A DA_base
rename D_A_se DA_se_base
rename xx DA_exp
rename xx_se DA_se_exp

gen dummy=0
foreach xx in DE EE FR GB GR HR HU SI {
display "`xx'"
replace dummy=1 if isocode=="`xx'"
}
keep if dummy==1

gen u_DA_base = DA_base + 1.96 * DA_se_base 
gen l_DA_base = DA_base - 1.96 * DA_se_base 
gen u_DA = DA_exp + 1.96 * DA_se_exp 
gen l_DA = DA_exp - 1.96 * DA_se_exp 

keep year isocode type u_DA_base l_DA_base u_DA l_DA DA_base DA_exp
foreach vv in u_DA_base l_DA_base u_DA l_DA DA_base DA_exp {
display "`vv'"
}
gsort DA_base
gen num=1 if isocode=="SI"
replace num=2 if isocode=="EE"
replace num=3 if isocode=="HR"
replace num=4 if isocode=="GR"
replace num=5 if isocode=="HU"
replace num=6 if isocode=="GB"
replace num=7 if isocode=="FR"
replace num=8 if isocode=="DE"
**gsort -num
drop year
gen t=_n
sum t

sum t
forvalues ii=1(1)`r(max)' {
local ctry`ii' = isocode[`ii']
local obs`ii' = `ii'
display "`ctry`ii'' ; `obs`ii''"
}


twoway rcap u_DA_base l_DA_base t, lc(red) /// 
||     rcap u_DA l_DA t, lc(blue) /// 
||     scatter DA_base t, mc(red)  /// 
||     scatter DA_exp t, mc(blue)  ///
yline(0) yline(0) ylab(-2(1)4) xlab(1(1)8)  xlabel(, valuelabel) xlabel(, angle(45)) ytitle("") xtitle("") graphregion(fcolor(white) lcolor(white)) xlabel(`obs1' "`ctry1'" `obs2' "`ctry2'"  `obs3' "`ctry3'" `obs4' "`ctry4'" `obs5' "`ctry5'" `obs6' "`ctry6'" `obs7' "`ctry7'" `obs8' "`ctry8'", labsize(small)) legend(order(3 "Baseline (adjusted)" 4 "Export correction") rows(1) size(small) bplace(n) ring(0) region(lwidth(none)))
display "fig1_`title'_`yy'"
graph export "${rappendix}/fig_exclexports_aggregate.pdf", replace

